<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>实例</title>
    <script type="text/javascript" src="../assets/js/vue-2.3.0.js"></script>
    <script type="text/javascript" src="../assets/js/jquery.js"></script>
</head>

<body>
    <h1>实例方法-事件</h1>
    <hr>
    <div id="app">
        {{num}}
        <p>
            <button @click="add()">加</button>
        </p>
    </div>
    <p>
        <button onclick="reduce()">减</button>
    </p>
    <p>
        <button onclick="reduceOnce()">减一次</button>
    </p>
    <p>
        <button onclick="off()">关闭</button>
    </p>
</body>
<script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data: {
            num: 6
        },
        methods: {
            add: function () {
                console.log('加了');
                this.num++
            }
        }
    })
    app.$on('reduce', function () {
        console.log('减了');
        this.num--;
    })

    function reduce() {
        app.$emit('reduce');
    }

    app.$once('reduceOnce', function () {
        console.log('只执行一次的方法');
        this.num--;
    });

    function reduceOnce() {
        app.$emit('reduceOnce');
    }

    function off() {
        app.$off('reduce');
    }
   

</script>

</html>